#include "graphwid.h"

GraphWid::GraphWid(QWidget *parent) : QWidget(parent)
{
        caretX = 0;
	caretY = 0;
	autoScroll = false;
	scaleH = 5.0;
	maxHR = 250.0;
	maxSpeed = 700.0;
	maxAlt = 1000.0;
	setMouseTracking(true);
	setMinimumHeight(300);
	isScrolling = false;
	hrVis = true;
  	altVis = true;
  	speedVis = true;
}

void GraphWid::setHRVisible(bool value)
{
	hrVis = value;
	update();
}

void GraphWid::setAltVisible(bool value)
{
	altVis = value;
	update();

}

void GraphWid::setSpeedVisible(bool value)
{
	speedVis = value;
	update();
}


void GraphWid::print(QPrinter *printer)
{
	printer->setOrientation(QPrinter::Landscape);
	QPainter painter;
	painter.begin(printer);
	drawPathColor(&painter, lineHR, Qt::SolidLine);
	painter.drawText(0, 0, "Heart Rate");
  	drawPathColor(&painter, lineSpeed, Qt::DashLine);
	painter.drawText(0, 50, "Speed");
  	drawPathColor(&painter, lineAlt, Qt::DashDotLine);
	painter.drawText(0, 100, "Altitude");
  	painter.end();
}

void GraphWid::resizeEvent(QResizeEvent *event) {
  lineHR = createColorPath(vectHR, 0.0, 160.0, 250.0, 
	QColor(Qt::blue), QColor(Qt::red));
  lineSpeed = createColorPath(vectSpeed, 0.0,  300.0, 700.0, 
	QColor(Qt::black), QColor(Qt::black));
  lineAlt = createColorPath(vectAlt, -300.0, 500.0, 1000.0, 
	QColor(Qt::green), QColor(Qt::darkRed));
}

void GraphWid::scrolling(bool isScrollingNew)
{
	isScrolling = not isScrollingNew;
	update(); 
}

void GraphWid::caretMoving(int x)
{
	if (x != -1) {
		autoScroll = true;
		caretX = x;
		caretY = vectHR->at((int)(caretX/scaleH));
		emit hrChanged(vectHR->at((int)(caretX/scaleH)));
		emit speedChanged((vectSpeed->at((int)(caretX/scaleH))/10));
		emit altChanged(vectAlt->at((int)(caretX/scaleH)));
		emit cronoChanged(caretX);
	}
	else {
		autoScroll = false;

	}
	update();	
}

void GraphWid::mouseMoveEvent(QMouseEvent *e)
{
	xtooltip = e->x();
	ytooltip = e->y();
	update();
	if (xtooltip > 0) {
		emit hrChanged(vectHR->at((int)(xtooltip/scaleH)));
		emit speedChanged((vectSpeed->at((int)(xtooltip/scaleH))/10));
		emit altChanged(vectAlt->at((int)(xtooltip/scaleH)));
		emit cronoChanged(xtooltip);
	}
	e->ignore();
}

void GraphWid::leaveEvent(QEvent* /* event */)
{
  xtooltip = -1;
  ytooltip = -1;
  update();
}

void GraphWid::mousePressEvent(QMouseEvent* /* event */)
{
  xtooltip = -1;
  ytooltip = -1;
  update();
}

QVector<ColorPath*>* GraphWid::createColorPath(QVector<int>* vect, qreal min, 
	qreal mid, qreal max, QColor midColor, QColor maxColor)
{
	if (vect != NULL) {
	QVector<ColorPath*> *line = new QVector<ColorPath*>;
	int i = 0;
	qreal scaleV = ((qreal)height()) / max;
	while (i < (vect->size() - 1))
	{
		qreal p = vect->at(i);
		QPainterPath *pp = new QPainterPath();
		pp->moveTo(QPointF(i * scaleH, height() - scaleV * p));
		int j = i + 1;
		qreal maxZone;
		qreal minZone;
		QColor col;
		
		if (vect->at(j) <= mid) {
			maxZone = mid;
			minZone = min;
			col = midColor;
		}
		else { 
			maxZone = max;
			minZone = mid + 1;
			col = maxColor;
		}
		while((vect->at(j) <= maxZone) && (vect->at(j) >= minZone)) {
			p = vect->at(j);
			pp->lineTo(QPointF(j * scaleH, height() - scaleV * p));
			j++;	
		}
		i = j - 1;
		ColorPath *cp = new ColorPath(pp, col);
		line->append(cp);
	}
	return line;
	}
	else return NULL;
}

void GraphWid::setData(DataHRM *data)
{
  vectHR = data->getHRData();
  vectSpeed = data->getSpeedData();
  vectAlt = data->getAltData();
  setFixedWidth((int)((vectHR->size()- 1) * scaleH));
  lineHR = createColorPath(vectHR, 0.0, 160.0, 250.0, 
	QColor(Qt::blue), QColor(Qt::red));
  lineSpeed = createColorPath(vectSpeed, 0.0,  300.0, 700.0, 
	QColor(Qt::black), QColor(Qt::black));
  lineAlt = createColorPath(vectAlt, -300.0, 500.0, 1000.0, 
	QColor(Qt::green), QColor(Qt::darkRed));
}

void GraphWid::drawPathColor(QPainter* painter, QVector<ColorPath*>* line, Qt::PenStyle style)
{
	if (line != NULL) {
		for (int i = 0; i < line->size(); i++) {
			ColorPath *cp = line->at(i);
			painter->setPen(QPen(cp->getColor(), 1, style));
			painter->drawPath(*cp->getPP());
		}
  	}
}

void GraphWid::paintEvent(QPaintEvent *)
{
  
  QPainter painter(this);

  if (hrVis || autoScroll) {	
	drawPathColor(&painter, lineHR, Qt::SolidLine);
  }
  if (speedVis) {
	drawPathColor(&painter, lineSpeed, Qt::SolidLine);
  }
  if (altVis) {;
	drawPathColor(&painter, lineAlt, Qt::SolidLine);
  }

  painter.setPen(Qt::DotLine);
  
  if (xtooltip != -1 && !isScrolling && !autoScroll) {
    painter.drawLine(xtooltip, 0, xtooltip, height());
  }
  
  if (autoScroll) {
    qreal scaleV = height() / maxHR;

    //QPixmap minibike(":/images/minibike.png");
    //painter.drawPixmap((int)(caretX - minibike.width() / 2), 
	//(int)(scaleV*(maxHR - caretY - minibike.height() / 2)), minibike);
  }
}

ColorPath::ColorPath(QPainterPath* pp, QColor col) {
  path = pp;
  color = col;
}

QColor ColorPath::getColor()
{
	return color;
}

QPainterPath* ColorPath::getPP()
{
	return path;
}
